module WindSpirit
{
    //背景音乐轮播封装
    export class BackMusic
    {
        private static _instance: BackMusic = null;
        public mp3Urls = [
            "http://www.windspirit.cn/weixin/games/resource/bgMusic.mp3",
            "http://www.windspirit.cn/weixin/games/resource/lianrenxin.mp3"
        ];
        public sounds: egret.Sound[] = [];
        public index = 0;
        public constructor()
        {
            if(BackMusic._instance == null)
            {
                BackMusic._instance = this;
                this.loadMp3();
            }
        }
        static get instance()
        {
            if(this._instance == null)
            {
                this._instance = new BackMusic;
            }
            return this._instance;
        }
        public complete(sound:egret.Sound)
        {
            this.sounds.push(sound);
            console.log("load mp3"+this.index+" complete");
            this.playSound();
            this.index++;
            this.index %= this.mp3Urls.length;
        }
        public loadMp3()
        {
            if(this.index < this.sounds.length)
            {
                this.index++;
                this.playSound();
            }
            else
            {
                RES.getResByUrl(this.mp3Urls[this.index],this.complete,this,RES.ResourceItem.TYPE_SOUND);
            }
        }
        public playSound()
        {
            this.sounds[this.index].play();
            this.sounds[this.index].addEventListener(egret.SoundEvent.COMPLETE,this.soundEnd,this);
        }
        public soundEnd()
        {
            console.log("sound play end");
        }
    }
}